//
//  FriendModel.m
//  project
//
//  Created by 黄建华 on 2018/8/11.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "FriendModel.h"

@implementation FriendModel

- (instancetype)init {
    self = [super init];
    if (self) {
        _nickName   = @"GetFitPro";
        _gender = 1;
        _sign       = SN_Text(@"这家伙很懒");
        _remark     = @"";
        _isBest     = NO;
        _isSupport  = NO;
        _showValue1 = @"0";
        _showValue2 = @"0";
        _showValue3 = @"0";
        _showValue4 = @"0";
        
        _showValue11 =@"";
        _showValue22 = @"";
        _showValue33 = @"";
        _showValue44 = @"";
        
        _heartMax = 0;
        _heartAve = 0;
        _heartMin = 0;
        _oxMax = 0;
        _oxMin = 0;
        _oxAve = 0;
        _sbpAve = 0;
        _dbpAve = 0;
    }
    return self;
}

+ (FriendModel *)getFriendModelWith:(NSString *)userId {
    NSString *where = [NSString stringWithFormat:@"user_id = '%@' and loginId =‘%@’ ",
                       userId, SN_USER.user_id];
    FriendModel *model = [FriendModel searchSingleWithWhere:where orderBy:nil];
    
    if (!model) {
        model         = [[FriendModel alloc] init];
        model.user_id = userId;
        [model updateToDB];
    }
    return model;
}

- (NSString *)showNickName {
    if (_remark.length == 0) {
        return _nickName;
    } else {
        if ([_remark isEqualToString:SN_Text(@"未填写备注")]) {
            return _nickName;
        } else {
            return _remark;
        }
    }
}

- (NSString *)showSign {
    if (_sign.length > 0) {
        return _sign;
    } else {
        return SN_Text(@"这家伙很懒");
    }
}

// 表名
+ (NSString *)getTableName {
    return @"FriendModel";
}

// 复合主键
+ (NSArray *)getPrimaryKeyUnionArray {
    return @[@"user_id", @"loginId"];
}

// 表版本
+ (int)getTableVersion {
    return 1;
}

+ (void)initialize {
    [super initialize];
}
- (void)updateToSelWith:(NSDictionary *)dic
{
    NSDictionary *dicN = dic[@"data"];
    NSInteger is_encourage = [dicN[@"is_encourage"]integerValue];
    NSInteger is_thumb = [dicN[@"is_thumb"]integerValue];
    self.isBest = is_thumb;
    self.isSupport = is_encourage;
    
    NSDictionary *dataDic = dicN[@"stat"];
    self.step = [dataDic[@"today_step"]integerValue];
    self.distance = [dataDic[@"today_distance"]integerValue];
    self.carlor = [dataDic[@"today_calory"]integerValue];
    self.sportduration = [dataDic[@"today_duration"]integerValue];
    NSDictionary *value1 = (NSDictionary *)[dataDic[@"max_step"] parseJSONString];
    NSDictionary *value2 = (NSDictionary *)[dataDic[@"finish_days"]parseJSONString];
    NSDictionary *value3 = (NSDictionary *)[dataDic[@"max_week"]parseJSONString];
    NSDictionary *value4 = (NSDictionary *)[dataDic[@"max_month"]parseJSONString];
    
    self.showValue1 = StrByInt([value1[@"value"]integerValue]);
    self.showValue2 = StrByInt([value2[@"value"]integerValue]);
    self.showValue3 = StrByInt([value3[@"value"]integerValue]);
    self.showValue4 = StrByInt([value4[@"value"]integerValue]);
    
    NSString *show1 = value1[@"date"];
    NSString *show2 = value2[@"date"];
    NSString *show3 = value3[@"date"];
    NSString *show4 = value4[@"date"];
    self.showValue11 = show1;
    self.showValue22 = show2;
    self.showValue33 = show3;
    self.showValue44 = show4;
    if (show1.length == 0) {
        self.showValue11 = SN_Text(@"暂无数据");
    }
    if (show2.length == 0) {
        self.showValue22 = SN_Text(@"暂无数据");
    }
    if (show3.length == 0) {
        self.showValue33 = SN_Text(@"暂无数据");
    }
    if (show4.length == 0) {
        self.showValue44 = SN_Text(@"暂无数据");
    }
    NSString *detailSportData = dataDic[@"sport_history"];
    NSArray *sportArr =  (NSArray *)[detailSportData parseJSONString];
    self.stepHistory = [NSArray arrayWithArray:sportArr];
    
    NSString *detailSleepData = dataDic[@"sleep_history"];
    NSArray *sleepArr =  (NSArray *)[detailSleepData parseJSONString];
    self.sleepHistory = [NSArray arrayWithArray:sleepArr];
    
    NSString *detailHeartData = dataDic[@"heart_history"];
    NSArray *heartArr =  (NSArray *)[detailHeartData parseJSONString];
    if (heartArr.count > 0) {
        NSDictionary *todayHeart = [heartArr lastObject];
        self.heartMax = [todayHeart[@"heartMax"]integerValue];
        self.heartMin = [todayHeart[@"heartMin"]integerValue];
        self.heartAve = [todayHeart[@"heartAve"]integerValue];
    }
    self.heartHistory = [NSArray arrayWithArray:heartArr];

    NSString *detailOxData = dataDic[@"ox_history"];
    NSArray *oxArr =  (NSArray *)[detailOxData parseJSONString];
    if (oxArr.count > 0) {
        NSDictionary *todayOx = [oxArr lastObject];
        self.oxMax = [todayOx[@"oxMax"]integerValue];
        self.oxMin = [todayOx[@"oxMin"]integerValue];
        self.oxAve = [todayOx[@"oxAve"]integerValue];
    }
    self.oxHistory = [NSArray arrayWithArray:oxArr];
    
    NSString *detailBpData = dataDic[@"bp_history"];
    NSArray *bpArr =  (NSArray *)[detailBpData parseJSONString];
    if (bpArr.count > 0) {
        NSDictionary *todayBp = [bpArr lastObject];
        self.sbpAve = [todayBp[@"SBPave"]integerValue];
        self.dbpAve = [todayBp[@"DBPave"]integerValue];
    }
    self.bpHistory = [NSArray arrayWithArray:bpArr];
    [self updateToDB];
}

@end
